<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Operation;

use App\Models\Basics\SysCountry;
use App\Models\Operation\TCTotal;
use App\Models\Operation\TCTotalFile;
use App\Models\Product\ProductClassify;
use App\Models\Operation\BATotal;
use Illuminate\Support\Facades\Artisan;
class TCTotalAjax {

    public function verifyTotalProfit($id)
    {
        $baTotal = BATotal::query()->find($id);
        $date = $baTotal->report_date;
        $countryId = $baTotal->country_id;
        $classifyId = $baTotal->classify_id;
        $total = TCTotal::query()
            ->where('report_date',$baTotal->report_date)
            ->where('country_id',$baTotal->country_id)
            ->where('classify_id',$baTotal->classify_id)
            ->first();
        $rateOdds = $total->rate_odds;
        $spend = $total->spend;
        $reimburse = $total->reimburse;
        $promotionDiscount = $total->promotion_discount;
        $pt = $total->profit;
        $basics = [];
        $basics['date'] = $date;
        $basics['country'] = SysCountry::getCountryById($countryId);
        $basics['title'] = ProductClassify::getTitleById($classifyId);
        $basics['rateOdds'] = $rateOdds;
        $prs = [];
        $profit = 0;
        foreach ($total->detail as $detail){
            $temp = [];
            $temp['psku_code'] = $detail->sku->psku_code ?? '';
            $temp['amount'] = $detail->amount;
            $volume = $detail->volume;
            $temp['volume'] = $volume;
            $temp['tpl'] = round($volume * $detail->tpl_rmb / $rateOdds,3);
            $temp['head_cost'] = $volume * $detail->head_cost;
            $temp['fba_fee'] = $volume * $detail->fba_fee;
            $temp['commission'] = $detail->commission;
            $temp['return'] = $detail->return;
            $temp['vat'] = $detail->vat;
            $temp['cost'] = $detail->cost;
            $temp['profit'] = $detail->profit;
            $_profit = round($detail->profit / $rateOdds,3);
            $temp['profit_rmb'] = $_profit;
            $profit += $_profit;
            $prs[$detail->psku_id] = $temp;
        }
        return view('operation.total.profit-modal', compact('basics','prs','profit','spend','reimburse','promotionDiscount','pt'));
    }


    public function createFile($param)
    {
        $start = $param['startDate'];
        $end = $param['endDate'];
        $countryId = $param['countryId'];
        $classifyId = $param['classifyId'];
        $file = TCTotalFile::getFile($start,$end,$countryId,$classifyId);
        if(is_file($file)){
            TCTotalFile::storeFile($start,$end,$countryId,$classifyId);
            return response()->json(['status' => true,'message' => '报表已生成！']);
        }
        try{
            Artisan::call('operation:check-file',
                ['start_date' => $start,'end_date' => $end,'country_id' => $countryId,'classify_id' => $classifyId]);
            return response()->json(['status' => true, 'message' => '报表生成完成！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误！']);
        }
    }
}
